package org.squareland;
import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;


public class ControllerView extends View {

	public ControllerView(Context context) {
		super(context);
		lock = new Object();
		setBackgroundColor(Color.BLUE);
		setFocusableInTouchMode(true);
		setFocusable(true);
	}
	
	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		height = h;
		width = w;
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		synchronized(lock) {
			if (event.getAction() == MotionEvent.ACTION_DOWN ||
				event.getAction() == MotionEvent.ACTION_MOVE) {
				x = event.getX() / width - 0.5f;
				y = event.getY() / height - 0.5f;
				Log.d("CONTROLLER", "Controller vctor is (" + x +", " + y + ")");
				return true;
			}
			if (event.getAction() == MotionEvent.ACTION_UP) {
				x = y = 0;
				Log.d("CONTROLLER", "Controller vctor is (" + x +", " + y + ")");
				return true;
			}
			Log.d("CONTROLLER", "Controller vctor is (" + x +", " + y + ")");
		}
		return super.onTouchEvent(event);
	}
	
	public void getCoords(Pointf point) {
		synchronized(lock) {
			if (width == 0 || height == 0) {
				point.x = 0;
				point.y = 0;
			} else {
				point.x = x;
				point.y = y;
			}
		}
	}
	
	private Object lock;
	private int height;
	private int width;
	private float x;
	private float y;
}
